//
//  BoxLoadingHelper.m
//  Kaagad Peso
//
//  Created by ZhengChengFeng on 2024/10/15.
//

#import "BoxLoadingHelper.h"

#define kTagView 70000

@implementation BoxLoadingHelper

+ (void)showBoxWithMessage:(NSString *)message{
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 14,*)) {
        window =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    [self showBoxWithMessage:message onView:window duration:3.5];
}

+ (void)showBoxWithMessage:(NSString *)message onView:(UIView *)view{
    [self showBoxWithMessage:message onView:view duration:3.5f];
}

+ (void)showBoxWithMessage:(NSString *)message onView:(UIView *)view duration:(NSTimeInterval)duration {
    
    CGSize size = CGSizeZero;
    if (PMStringIsEmpty(message)) {
        size = CGSizeMake(60, 60);
    }
    else{
        size = [message boundingRectWithSize:CGSizeMake(view.bounds.size.width - 80, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size;
    }
   
    CGFloat width = size.width + 40;
    CGFloat height = size.height + 30;
    
    UIView * textView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    textView.backgroundColor = [UIColor blackColor];
    textView.alpha = 1;
    textView.layer.cornerRadius = 10;
    textView.layer.masksToBounds = YES;
    textView.center = CGPointMake(view.bounds.size.width * 0.5, view.bounds.size.height * 0.5);
    
    UILabel * messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, size.width, size.height)];
    messageLabel.font = [UIFont systemFontOfSize:16];
    messageLabel.textColor = [UIColor whiteColor];
    messageLabel.text = esString(message);
    messageLabel.numberOfLines = 0;
    [textView addSubview:messageLabel];
    [view addSubview:textView];
    messageLabel.textAlignment = 1;
    
    [UIView animateWithDuration:duration animations:^{
        textView.alpha = 0;
    } completion:^(BOOL finished) {
        [textView removeFromSuperview];
    }];
}


+ (void)showBoxLoadingWithMessage:(NSString *)message{
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 14,*)) {
        window =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    [self showBoxLoadingWithMessage:message onView:window withOriginY:0];
}

+ (void)showBoxLoadingWithMessage:(NSString *)message onView:(UIView *)view withOriginY:(CGFloat)originY{
    
    UIView *shadeV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    shadeV.backgroundColor = [UIColor clearColor];
    shadeV.tag = kTagView;
    [view addSubview:shadeV];
    
    
    CGFloat indicatorWidth = 40;
    CGFloat waitingIconSize = 70;
    
    UIView *boxView = [[UIView alloc] init];
    boxView.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    boxView.alpha = 0.85;
    

    boxView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    

    boxView.frame = CGRectMake((kScreenWidth - waitingIconSize)/2, (kScreenHeight - waitingIconSize)/2 + originY, waitingIconSize, waitingIconSize);
    
    boxView.layer.cornerRadius = 8;
    boxView.layer.masksToBounds = YES;
    
    [shadeV addSubview:boxView];
    
    
    UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [indicatorView startAnimating];
    [boxView addSubview:indicatorView];
    
    UILabel *lab  = [[UILabel alloc] init];
    lab.textColor = [UIColor whiteColor];
    lab.font = [UIFont systemFontOfSize:15];
    lab.text = esString(message);
    lab.textAlignment = 1;
    [boxView addSubview:lab];
    
    [indicatorView setFrame:CGRectMake((waitingIconSize - indicatorWidth) / 2, (waitingIconSize - indicatorWidth)/2, indicatorWidth, indicatorWidth)];
}


+ (void)hideBoxLoadingFromView:(UIView *)view{
    UIView * boxView = [view viewWithTag:kTagView];
    [UIView animateWithDuration:0.25f animations:^{
    } completion:^(BOOL finished) {
        [boxView removeFromSuperview];
    }];
}

+ (void)hideBoxLoading{
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 14,*)) {
        window =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    [self hideBoxLoadingFromView:window];
}

@end
